package me.zhangsanfeng.base.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.zhangsanfeng.base.entity.Product;
import me.zhangsanfeng.base.service.ProductService;
import me.zhangsanfeng.frame.entity.Result;
import me.zhangsanfeng.frame.util.DataUtil;
import me.zhangsanfeng.frame.util.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author ZhangYao
 * @since 2018-10-18
 */
@Api(value = "基础信息模块")
@Slf4j
@RestController
@RequestMapping("/base/product")
public class ProductController {

    @Autowired
    private ProductService service;

    @ApiOperation(value = "新建记录",notes = "传入实体")
    @PostMapping
    public Result saveOne(@RequestBody Product product){
        log.info("controller-save()参数{}",product);
        return ResultUtils.success(service.insertProduct(product));
    }

    @ApiOperation(value = "批量新建记录",notes = "传入多个实体")
    @PostMapping("/batch")
    public Result saveBatch(@RequestBody List<Product> products){
        log.info("controller-saveBatch()参数{}",products);
        return ResultUtils.success(service.insertBatchProduct(products));
    }

    @ApiOperation(value = "删除记录")
    @DeleteMapping
    public Result deleteOne(@RequestBody Product product){
        log.info("controller-delete()参数{}",product);
        return ResultUtils.success(service.deleteProduct(product));
    }

    @ApiOperation(value = "批量删除记录",notes = "必须传入id")
    @DeleteMapping("/batch")
    public Result deleteBatch(@RequestBody List<Product> products){
        log.info("controller-deleteBatch()参数{}",products);
        return ResultUtils.success(service.deleteBatchProduct(products));
    }

    @ApiOperation(value = "修改记录",notes = "必须传入id")
    @PutMapping
    public Result updateOne(@RequestBody Product product){
        log.info("controller-update()参数{}",product);
        return ResultUtils.success(service.update(product));
    }

    @ApiOperation(value = "批量修改记录",notes = "必须传入id")
    @PutMapping("/batch")
    public Result updateBatch(@RequestBody List<Product> products){
        log.info("controller-updateBatch()参数{}",products);
        return ResultUtils.success(service.updateBatchProduct(products));
    }

    @ApiOperation(value = "查询记录")
    @PostMapping("/selectOne")
    public Result seleteOne(@RequestBody(required = false) Product product){
        log.info("controller-select()参数{}",product);
        return ResultUtils.success(service.selectProduct(product));
    }

    @ApiOperation(value = "查询列表",notes = "参数不能为空")
    @PostMapping("/selectList")
    public Result selectList(@RequestBody Product product){
        log.info("controller-selectList()参数{}",product);
        if (DataUtil.isNotEmpty(product.getPageSize())) {
            return ResultUtils.success(service.selectPageProduct(product));
        }

        return ResultUtils.success(service.selectListProduct(product));
    }
}

